package org.common.utils.fasttest;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.TypeReference;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import org.eclipse.collections.impl.lazy.parallel.Batch;
import org.common.utils.fasttest.entity.RoleProtagonistBattlePlanModel;
import org.common.utils.fasttest.entity.RoleResourceModel;
import org.common.utils.fasttest.entity.RoleStatisticsModel;
import org.common.utils.fasttest.entity.codec.RedissonTypedJacksonCodec;
import org.redisson.Redisson;
import org.redisson.api.*;
import org.redisson.client.codec.BaseCodec;
import org.redisson.client.protocol.Decoder;
import org.redisson.client.protocol.Encoder;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.ExecutionException;

/**
 * @author zhouzhibing
 * @date 2024/4/22
 */
public class TestRedis {


    /**
     * redis中key持续时间，这里为1天。
     */
    protected final static Duration DURATION = Duration.ofDays(1);
    private static final RedissonClient redissonClient = defaultRedissonClient();
    protected static final RedissonTypedJacksonCodec TYPE_CODEC = new RedissonTypedJacksonCodec(Long.class, RoleProtagonistBattlePlanModel.class);


    private static RedissonClient defaultRedissonClient() {

        Config config = new Config();
//        config.setCodec(new FastjsonCodec());
        config.useSingleServer().setAddress("redis://10.30.126.161:6379");
        return Redisson.create(config);
    }

    private static Map<Long, RoleProtagonistBattlePlanModel> getMap(List<RoleProtagonistBattlePlanModel> modelList) {
        Map<Long, RoleProtagonistBattlePlanModel> modelMap = new HashMap<>();
        for (RoleProtagonistBattlePlanModel model : modelList) {
            modelMap.put(model.getId() , model);
        }
        return modelMap;
    }

    public static void test() {

        LoggerContext loggerContext =  (LoggerContext) LoggerFactory.getILoggerFactory();
        for (ch.qos.logback.classic.Logger logger : loggerContext.getLoggerList()) {
            logger.setLevel(Level.OFF);
        }

//        Collection<Object> objects = redissonClient.getMap("role_mail:1813204904794263552" , TYPE_CODEC).readAllValues();
//        System.out.println();
//        List<RoleProtagonistBattlePlanModel> modelList = new ArrayList<>();
//        for (int i = 0; i < 100 ; i++) {
//            RoleProtagonistBattlePlanModel model = new RoleProtagonistBattlePlanModel();
//            model.setId(i);
//            model.setPlanName("RoleProtagonistBattlePlanModel" + i);
//
//            modelList.add(model);
//        }
//
//        Test.doHandler("RBatch.expireAsync" , 100 , 10 , (index) -> {
//            for (RoleProtagonistBattlePlanModel model : modelList) {
//                RMap<Long , RoleProtagonistBattlePlanModel> map =  redissonClient.getMap("TestRedis：role_protagonist_battle_plan:"+model.getRoleId() , TYPE_CODEC);
//                map.fastPutAsync(model.getId() , model);
//                map.expireAsync(DURATION);
//            }
//        });

//        Test.doHandler("getMap.put" , 100 , 10 , (index) -> {
//            RMap<Long , RoleProtagonistBattlePlanModel> map =  redissonClient.getMap("TestRedis：role_protagonist_battle_plan:"+index , TYPE_CODEC);
//            for (RoleProtagonistBattlePlanModel model : modelList) {
//                map.put(model.getId() , model);
//            }
//        });

//        Test.doHandler("getMap.putAll" , 100 , 10 , () -> {
//            Map<Long, RoleProtagonistBattlePlanModel> modelMap = getMap(modelList);
//            RMap<Long , RoleProtagonistBattlePlanModel> map =  redissonClient.getMap("TestRedis：role_protagonist_battle_plan:test" , TYPE_CODEC);
//            map.putAll(modelMap);
//        });

//        Test.doHandler("getMap.putAllAsync" , 100 , 10 , () -> {
//            RMap<Long , RoleProtagonistBattlePlanModel> map =  redissonClient.getMap("TestRedis：role_protagonist_battle_plan:test" , TYPE_CODEC);
//            map.putAllAsync(getMap(modelList));
//        });



//        Test.doHandler("RBatch.fastPutAsync.execute" , 100 , 10 , (index) -> {
//            RBatch batch = redissonClient.createBatch();
//            for (RoleProtagonistBattlePlanModel model : modelList) {
//                RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("TestRedis：role_protagonist_battle_plan:"+model.getRoleId() , TYPE_CODEC);
//                map.fastPutAsync(model.getId() , model);
//            }
//            batch.execute();
//        });

//        Test.doHandler("RBatch.fastPutAsync.executeAsync" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            for (RoleProtagonistBattlePlanModel model : modelList) {
//                map.fastPutAsync(model.getId() , model);
//            }
//            batch.executeAsync();
//        });

//        Test.doHandler("RBatch.putAsync.execute()" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            for (RoleProtagonistBattlePlanModel model : modelList) {
//                map.putAsync(model.getId() , model);
//            }
//            batch.execute();
//        });

//        Test.doHandler("RBatch.putAsync.executeAsync()" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            for (RoleProtagonistBattlePlanModel model : modelList) {
//                map.putAsync(model.getId() , model);
//            }
//            batch.executeAsync();
//        });

//        Test.doHandler("RBatch.putAsync.executeAsync().get()" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            for (RoleProtagonistBattlePlanModel model : modelList) {
//                map.putAsync(model.getId() , model);
//            }
//            try {
//                batch.executeAsync().get();
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        });



//        Test.doHandler("RBatch.fastPutAsync.execute()" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            for (RoleProtagonistBattlePlanModel model : modelList) {
//                map.fastPutAsync(model.getId() , model);
//            }
//            batch.execute();
//        });

//        Test.doHandler("RBatch.fastPutAsync.executeAsync()" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            for (RoleProtagonistBattlePlanModel model : modelList) {
//                map.fastPutAsync(model.getId() , model);
//            }
//            batch.executeAsync();
//        });

//        Test.doHandler("RBatch.fastPutAsync.executeAsync().get()" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            for (RoleProtagonistBattlePlanModel model : modelList) {
//                map.fastPutAsync(model.getId() , model);
//            }
//            try {
//                batch.executeAsync().get();
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        });


//        Test.doHandler("RBatch.putAllAsync.execute()" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            map.putAllAsync(modelMap);
//            batch.execute();
//        });

//        Test.doHandler("RBatch.putAllAsync.executeAsync()" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            map.putAllAsync(modelMap);
//            batch.executeAsync();
//        });

//        Test.doHandler("RBatch.putAllAsync.executeAsync().get()" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            map.putAllAsync(modelMap);
//            try {
//                batch.executeAsync().get();
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        });

//        Test.doHandler("RBatch.putAllAsync.executeAsync" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            map.putAllAsync(modelMap);
//            batch.executeAsync();
//        });

//        Test.doHandler("RBatch.putAllAsync.executeAsync.get()" , 100 , 10 , () -> {
//            RBatch batch = redissonClient.createBatch();
//            RMapAsync<Long , RoleProtagonistBattlePlanModel> map =  batch.getMap("role_protagonist_battle_plan:test" , TYPE_CODEC);
//            map.putAllAsync(modelMap);
//            try {
//                batch.executeAsync().get();
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        });



//        RBatch batch1 = redissonClient.createBatch();
//        for (int i = 0; i < 10; i++) {
//            RoleResourceModel model = new RoleResourceModel();
//            model.setRoleId(i);
//            model.setGold(123456);
//            model.setDiamond("diamond");
//            RBucketAsync<RoleResourceModel> bucket = batch1.getBucket("RoleResourceModel" + i , codec);
//            bucket.setAsync(model , Duration.ZERO);
//        }
//        // 执行批量操作
//        batch1.execute();
//
//        RBatch batch2 = redissonClient.createBatch();
//        for (int i = 0; i < 10; i++) {
//            batch2.getBucket("RoleResourceModel" + i , codec).deleteAsync();
//        }
//        BatchResult<Boolean> batchResult = (BatchResult<Boolean>) batch2.execute();
//        List<Boolean> list = batchResult.getResponses();
//        int successCount = 0;
//        for (Boolean success : list) {
//            if(success)
//                successCount++;
//        }
//        int result = successCount == list.size() ? 1 : 0;
//        System.out.println();

//        RBatch batch = redissonClient.createBatch();
//        RMapAsync<Long , RoleResourceModel> batchMap = batch.getMap("map",codec);
//        for (int i = 0; i < 10; i++) {
//            RoleResourceModel model = new RoleResourceModel();
//            model.setRoleId(i);
//            model.setGold(123456);
//            model.setDiamond("diamond");
//            batchMap.putAsync(model.getRoleId() , model);
//        }
//        // 执行批量操作
//        int result = batch.execute().getResponses().size();
//        System.out.println("result = " + result);

//        RBatch batch1 = redissonClient.createBatch();
//        RMapAsync<Long , RoleResourceModel> batchMap1 = batch1.getMap("map",codec);
//        for (long i = 0; i < 10; i++) {
//            batchMap1.removeAsync(i);
//        }
//        // 执行批量操作
//        BatchResult<Boolean> batchResult = (BatchResult<Boolean>) batch1.execute();
//        int result1 = batchResult.getResponses().size();
//        System.out.println("result1 = " + result1);



//        RoleResourceModel model = new RoleResourceModel();
//        model.setRoleId(11111);
//        model.setGold(123456789);
//        model.setDiamond("diamond");
//        RMap<Object, Object> map = redissonClient.getm.getMap("map", codec);
//        map.expire(Duration.ZERO)
//        map.put(111 , model);


//        long result1 = redissonClient.getMap("map" , codec).fastRemove(1L);
//        System.out.println("result1 = " + result1);
//        RBatch batch = redissonClient.createBatch();
//        batch.getMap("ssss" , codec).removeAsync("");
//        batch.getBucket("ssss" , codec).deleteAsync();
//        batch.execute();

//        redissonClient.getBucket("ssss" , codec).delete();
    }
}



